package com.woniu.car.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.woniu.car.common.ResponseEntity;
import com.woniu.car.exception.ParamException;
import com.woniu.car.exception.ServiceSException;
import com.woniu.car.client.PlatformClient;
import com.woniu.car.domain.TOrderGoods;
import com.woniu.car.domain.TProductComment;
import com.woniu.car.mapper.TOrderGoodsMapper;
import com.woniu.car.mapper.TProductCommentMapper;
import com.woniu.car.dto.GoodsCommentDto;
import com.woniu.car.param.AddCommentByUserParam;
import com.woniu.car.param.DelCommentParam;
import com.woniu.car.param.GoodsCommentInsterParam;
import com.woniu.car.param.QueryGoodsCommentParam;
import com.woniu.car.service.TProductCommentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import com.woniu.car.Util.CommonConstant;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author chestnuts
 * @since 2021-06-21
 */
@Service
public class TProductCommentServiceImpl extends ServiceImpl<TProductCommentMapper, TProductComment> implements TProductCommentService {

    @Resource
    private TProductCommentMapper productCommentMapper;

    @Resource
    private TOrderGoodsMapper orderGoodsMapper;

    @Resource
    private PlatformClient platformClient;


    @Override
    public ResponseEntity insertComment(GoodsCommentInsterParam goodsCommentInsterParam) throws ParamException {

        TOrderGoods order = orderGoodsMapper.selectById(goodsCommentInsterParam.getOrderId());

        //这是保存到数据库的对象
        TProductComment productComment = new TProductComment();

        //这是返回前端的对象
        GoodsCommentDto goodsCommentDto = new GoodsCommentDto();

        Integer userId = order.getUserId();

        BeanUtils.copyProperties(order,productComment);


        productComment.setUserName("");

        productComment.setCommentInfo(goodsCommentInsterParam.getCommentInfo());
        productComment.setCommentScore(goodsCommentInsterParam.getCommentScore());

        productComment.setCommentTime(Long.toString(System.currentTimeMillis()));
        productComment.setOverTime(order.getOverTime());
        productComment.setOrderNo(order.getOrderNumber());
        productComment.setStatus(0);

        int insert = productCommentMapper.insert(productComment);

        QueryWrapper<TProductComment> wrapper = new QueryWrapper<>();
        wrapper.eq("comment_time",productComment.getCommentTime());

        TProductComment comment = productCommentMapper.selectOne(wrapper);

        return new ResponseEntity().setCode(500).setMsg("评论成功").setObject(comment);
    }

    @Override
    public ResponseEntity addCommentByUser(AddCommentByUserParam addCommentByUserParam) {


        UpdateWrapper<TProductComment> wrapper = new UpdateWrapper<>();

        wrapper.set("add_comment",addCommentByUserParam.getAddComment())
                .set("comment_time",Long.toString(System.currentTimeMillis()))
                .set("status",1)
                .eq("order_id",addCommentByUserParam.getOrderId());

        boolean update = this.update(wrapper);

        TProductComment tProductComment = productCommentMapper.selectById(addCommentByUserParam.getOrderId());

        return new ResponseEntity().setCode(200).setMsg("追评成功").setObject(tProductComment);
    }

    @Override
    public ResponseEntity queryComment(QueryGoodsCommentParam queryGoodsCommentParam) throws ParamException, ServiceSException {
        if(null == queryGoodsCommentParam.getUserId())return new ResponseEntity().setCode(503).setMsg("用户id不能为空");


        if (
                null == queryGoodsCommentParam.getOrderId() &&
                null == queryGoodsCommentParam.getUserId() &&
                null == queryGoodsCommentParam.getStatus()){
            throw new ParamException(CommonConstant.COMMENTQUERYWRONG);
        }


        QueryWrapper<TProductComment> wrapper = new QueryWrapper();


        if (null != queryGoodsCommentParam.getOrderId()) {
            wrapper.eq("order_id",queryGoodsCommentParam.getOrderId());
        }
        if (null != queryGoodsCommentParam.getOrderId()) {
            wrapper.eq("status",queryGoodsCommentParam.getStatus());
        }
        wrapper.eq("user_id",queryGoodsCommentParam.getUserId());

        List<TProductComment> list = productCommentMapper.selectList(wrapper);


        if (null == list || list.size() == 0) return new ResponseEntity().setMsg("查询无果").setCode(200);

        return new ResponseEntity().setObject(list).setMsg("查询成功").setCode(200);
    }

    @Override
    public ResponseEntity delComment(DelCommentParam delCommentParam) {
        List<Integer> list = delCommentParam.getCommentIdList();
        List<TProductComment> commentList = new ArrayList<>();
        for(int i:list){
            TProductComment tProductComment = productCommentMapper.selectById(i);
            int c = productCommentMapper.deleteById(i);
            commentList.add(tProductComment);
        }
        return new ResponseEntity().setMsg("").setObject(commentList).setCode(200);
    }
}
